﻿using BCCommon;
using System;
using System.Collections.Generic;
using System.Net.WebSockets;
using System.Text;
using System.Threading.Tasks;

namespace BCService
{
    [NonInjection]
    public interface IWebSocketContextService : IDisposable
    {
        /// <summary>
        /// 用户已连接。
        /// </summary>
        event Action OnConnected;

        /// <summary>
        /// 收到文本消息。
        /// </summary>
        event Action<string> OnTextReceived;

        /// <summary>
        /// 收到二进制消息。
        /// </summary>
        event Action<byte[]> OnBinaryReceived;

        /// <summary>
        /// 用户已断开。
        /// </summary>
        event Action OnClosed;

        /// <summary>
        /// 尝试监听连接。
        /// </summary>
        /// <returns></returns>
        Task TryListenAsync();

        /// <summary>
        /// 尝试关闭连接。
        /// </summary>
        /// <param name="closeStatus">关闭状态码。</param>
        /// <param name="statusDescription">状态说明。</param>
        /// <returns></returns>
        Task TryCloseAsync(WebSocketCloseStatus closeStatus, string statusDescription);

        /// <summary>
        /// 发送文本数据。
        /// </summary>
        /// <param name="text">文本内容。</param>
        /// <param name="chatUserId">接收用户，如果不指定则发送给自己。</param>
        /// <returns></returns>
        Task<bool> SendTextAsync(string text, string chatUserId = null);

        /// <summary>
        /// 发送二进制数据。
        /// </summary>
        /// <param name="bytes">二进制数据。</param>
        /// <param name="chatUserId">接收用户，如果不指定则发送给自己。</param>
        /// <returns></returns>
        Task<bool> SendBinaryAsync(byte[] bytes, string chatUserId = null);

        /// <summary>
        /// 检查用户是否在线。
        /// </summary>
        /// <param name="chatUserId">沟通人Id</param>
        /// <returns></returns>
        Task<bool> CheckOnlineAsync(string chatUserId);
    }
}
